
package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.Select;
import ru.esteru.selenium.factory.WebDriverFactory;


import com.opera.core.systems.OperaDriver;


public class HW1 {
  private WebDriver driver;
  private String baseUrl;
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
	webDriverFactory.
    driver = new OperaDriver();
    baseUrl = "http://localhost/";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testHW1() throws Exception {
	  goToMainPage();
    loginAs("admin", "admin");
    loguot();
  }
  
  @Test
  public void testNewUser() {
	  goToMainPage();
	  loginAs("admin", "admin");
	  goToUserManagementConsole();
	  createNewUser("ikhmelik", "ikhmelik@gmail.com", "123123");
	  loguot();
	  
  
  }
  
  private void createNewUser(String usename, String email, String password) {
	  driver.findElement(By.id("username")).clear();
	    driver.findElement(By.id("username")).sendKeys(usename);
	    driver.findElement(By.name("email")).clear();
	    driver.findElement(By.name("email")).sendKeys(email);
	    driver.findElement(By.id("password")).clear();
	    driver.findElement(By.id("password")).sendKeys(password);
	    driver.findElement(By.id("password2")).clear();
	    driver.findElement(By.id("password2")).sendKeys(password);
	    driver.findElement(By.name("submit")).click();
	
}

private void goToUserManagementConsole() {
	 driver.findElement(By.linkText("User management")).click();
	
}

@Test
  public void testFilers() {
	  goToMainPage();
	  loginAs("admin", "admin");
	  filterByStatus();
	  filterByCategory();
	  filterByItem();
	  sorting();
      loguot();
  }
    
    
    private void sorting() {
    	driver.findElement(By.cssSelector("#sort-button > span.ui-selectmenu-status")).click();
	
}

	private void filterByItem() {
    	 driver.findElement(By.id("ui-selectmenu-item-995")).click();
	
}

	private void filterByCategory() {
		driver.findElement(By.xpath("//a[@id='category-button']/span[2]")).click();
	
}

	private void filterByStatus() {
		driver.findElement(By.cssSelector("span.ui-selectmenu-status")).click();
}

	@Test
    public void testSwithLanguage() {
    	goToMainPage();
    	loginAs("admin", "admin");
    	switchLanguage();
    	findFilm("Matrix");
      loguot();
    }
      
private void findFilm(String string) {
	driver.findElement(By.id("q")).clear();
    driver.findElement(By.id("q")).sendKeys("Matrix");
		
	}

private void switchLanguage() {
	  driver.findElement(By.linkText("Nederlands")).click();
		
	}

private void loguot() {
    driver.findElement(By.linkText("Log out")).click();
    assertTrue(closeAlertAndGetItsText().matches("^Are you sure you want to log out[\\s\\S]$"));
}

private void loginAs(String username, String password) {
    driver.findElement(By.id("username")).clear();
    driver.findElement(By.id("username")).sendKeys(username);
    driver.findElement(By.name("password")).clear();
    driver.findElement(By.name("password")).sendKeys(password);
    driver.findElement(By.name("submit")).click();
}

private void goToMainPage() {
    driver.get(baseUrl + "/php4dvd/");
}

  @After
  public void tearDown() throws Exception {
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

  private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alert.getText();
    } finally {
      acceptNextAlert = true;
    }
  }
}
